package com.demo.code;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CodeLineCounter {
    /**
     * 统计总代码行数
     * @param args
     */
    public static void main(String[] args) {
        String folderPath = "D:\\data\\test_template\\tool"; // 替换为你的文件夹路径
        int totalLines = countLinesInFolder(folderPath);
        System.out.println("总代码行数: " + totalLines);
    }

    public static int countLinesInFolder(String folderPath) {
        File folder = new File(folderPath);
        int totalLines = 0;
        if (folder.exists() && folder.isDirectory()) {
            for (File file : folder.listFiles()) {
                if (file.isFile()) {
                    totalLines += countLinesInFile(file);
                } else if (file.isDirectory()) {
                    totalLines += countLinesInFolder(file.getAbsolutePath());
                }
            }
        }
        return totalLines;
    }

    public static int countLinesInFile(File file) {
        int totalLines = 0;
        try (Scanner scanner = new Scanner(file)) {
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
                totalLines++;

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return totalLines;
    }
}
